#!/usr/bin/python
#Drew Carpenter & Jeff Gullett
#CS 4444
#Excavare 0.0
#March 29, 2011
import sys
import pygtk
import gtk
import makefilter
import os
import drawable
import pdfRead
import search_files
import docxRead
import file_io
import ocr_initialize
from open_workspace import *
from create_tabs import *
#to update glade xml file use this command:
#gtk-builder-convert tutorial.glade tutorial.xml

class ExcavareMain:
	def on_Excavare_destroy(self, widget, data=None):
	 	"""Destroys the excavare window"""
		gtk.main_quit()
	def on_ocr_tool_clicked(self,ocr_tool):
		"""Performs OCR on current tab."""
		ocr_initialize.ocr_initialize(self,ocr_tool)
	def on_ocr_activate(self,ocr_menu_item):
		"""Performs OCR on current tab."""
		ocr_initialize.ocr_initialize(self,ocr_menu_item)
	def on_save_file_clicked(self,widget):
		file_io.save_file(self,widget)
	def on_save_menu_item_activate(self,save_menu_item):
		file_io.save_file(self,save_menu_item)
	def on_save_as_menu_item_activate(self,save_as_menu_item):
		file_io.save_as_file(self,save_as_menu_item)
	def on_open_menu_item_activate(self,open_menu_item):
		"""Creates an open dialog box from which files can be chosen"""
		file_io.open_file(self,open_menu_item)
	def on_open_file_clicked(self,open_menu_item):		
		"""Creates an open dialog box from which files can be chosen"""
		file_io.open_file(self,open_menu_item)		
	def on_save_workspace_item_activate(self,save_workspace_item):
		file_io.save_workspace(self,save_workspace_item)
	def on_new_menu_item_activate(self,new_menu_item):
		file_io.new_file(self,new_menu_item)
	def on_new_file_clicked(self,new_menu_item):
		file_io.new_file(self,new_menu_item)
	def on_search_activate(self,widget):
		self.search_dialog.run()
		return
	def on_treeview1_row_activated(self,treeview,something,something2):
		"""This function is called when a row is clicked in the treeview"""
		list_sel = self.treeview.get_selection()
		(tm, ti) = list_sel.get_selected()
		###NEW NEEDS TESTING
		if tm.get_value(ti,1) is -1:
			create_new_tab_from_treeview(self,tm.get_value(ti,2),tm,ti)
		###END NEW	
		self.notebook.set_current_page((tm.get_value(ti, 1)))
		self.notebook.show_all()
	def on_prev_button_clicked(self,prev_button):
		search_files.prev_word(self)
		return
	def on_back_button_clicked(self,back_button):
		current=self.notebook.get_current_page()
		tm=self.treeview.get_model()
		iter=tm.get_iter(current)
		num_pages=tm.get_value(iter,3)
		self.current_page=tm.get_value(iter,4)
		if self.current_page==0:
			return
		else:
			self.current_page=self.current_page-1
			tm.set_value(iter,4,self.current_page)			
			filename=tm.get_value(iter,2)
			filename=filename+"x"
			temp_filename=list(filename)
			temp_filename[-5]=str(self.current_page)
			temp_filename[-4]="."
			temp_filename[-3]="t"
			temp_filename[-2]="i"
			temp_filename[-1]="f"
			filename="".join(temp_filename)
			scrolly=self.notebook.get_focus_child()
			view=scrolly.get_child()
			event=view.get_child()
			image = gtk.Image()
			image.set_alignment(0,0)
			image.set_padding(0,0)
			picture = gtk.gdk.pixbuf_new_from_file(filename)
			w,h = picture.get_width(), picture.get_height()
			drawable = gtk.gdk.Pixmap(None, w, h, 24)
			gc = drawable.new_gc()
			drawable.draw_pixbuf(gc, picture, 0,0,0,0,-1,-1)
			image.set_from_pixmap(drawable,None)
			child=event.get_child()
			event.remove(child)
			event.add(image)
			event.queue_draw()
			self.notebook.show_all()
	def on_next_button_clicked(self,next_button):
		search_files.next_word(self)
		return
		
	def on_forward_button_clicked(self,forward_button):
		current=self.notebook.get_current_page()
		tm=self.treeview.get_model()
		iter=tm.get_iter(current)
		num_pages=tm.get_value(iter,3)
		self.current_page=tm.get_value(iter,4)		
		if self.current_page==(num_pages-1):
			return
		else:
			self.current_page+=1
			tm.set_value(iter,4,self.current_page)
			filename=tm.get_value(iter,2)
			filename=filename+"x"
			temp_filename=list(filename)
			temp_filename[-5]=str(self.current_page)
			temp_filename[-4]="."
			temp_filename[-3]="t"
			temp_filename[-2]="i"
			temp_filename[-1]="f"
			filename="".join(temp_filename)
			scrolly=self.notebook.get_focus_child()
			view=scrolly.get_child()
			event=view.get_child()
			image = gtk.Image()
			image.set_alignment(0,0)
			image.set_padding(0,0)
			#print "\npixbuf file: "+filename+"\n"
			picture = gtk.gdk.pixbuf_new_from_file(filename)
			w,h = picture.get_width(), picture.get_height()
			drawable = gtk.gdk.Pixmap(None, w, h, 24)
			gc = drawable.new_gc()
			drawable.draw_pixbuf(gc, picture, 0,0,0,0,-1,-1)
			image.set_from_pixmap(drawable,None)
			child=event.get_child()
			event.remove(child)
			event.add(image)
			event.queue_draw()
			self.notebook.show_all()

	def on_closetab_button_clicked(self, sender, widget):
		#get the page number of the tab we wanted to close
		pagenum = self.notebook.page_num(widget)
		model=self.treeview.get_model()
		iter=model.get_iter_first()
		tabNumber=model.get_value(iter,1)
		i=0
		for i in range(0,self.tab_nums):
			if tabNumber is pagenum:
				model.set_value(iter,1,-1)
				break
			else:
				iter=model.iter_next(iter)
				tabNumber=model.get_value(iter,1)
		for i in range(i,self.tab_nums):
			iter=model.iter_next(iter)
			tabNumber2=model.get_value(iter,1)
			model.set_value(iter,1,(tabNumber2-1))
		self.tab_nums-=1
		#and close it
		self.notebook.remove_page(pagenum)	
	def __init__(self):
		builder = gtk.Builder()
		builder.add_from_file("excavareF.glade")
		#get main window Excavare and textview
		self.excavare = builder.get_object("Excavare")
		self.excavare.set_title("Excavare")
		self.excavare.maximize()
		#self.textview = builder.get_object("textview")
		#self.textview.set_left_margin(50)
		#self.textview.set_right_margin(50)
		#get the treeview and set its model to a liststore with strings
		self.treeview = builder.get_object("treeview1")
		#create a list store which holds tab name, tab number, filename, 
		#number of pages, current page and whether the file has been saved once
		self.list_store = gtk.ListStore(str,int,str,int,int,bool)
		self.treeview.set_model(self.list_store)
		#get the notebook so we can add pages to it when needed
		self.notebook = builder.get_object("notebook1")
		self.search_dialog = builder.get_object("dialog")
		self.search_entry=builder.get_object("entr")
		self.search_success=False
		
		#NEW
		self.notebook.set_property("scrollable",True)
		self.notebook.set_property("homogeneous",True)
		#
		#put a column named Files in the tree
		self.column = gtk.TreeViewColumn('Files')
		self.treeview.append_column(self.column)
		#get a CellRenderer and pack it, set the column attributes
		self.cell = gtk.CellRendererText()
		self.column.pack_start(self.cell, True)
		self.column.set_attributes(self.cell, text=0)
		#set the number of tabs to 1 and add text editor to list
		self.tab_nums = -1
		self.current_page=0
		self.is_workspace=False
		builder.connect_signals(self)
		self.textviews = []
		self.doing_ocr=False

if __name__ == "__main__":
	program = ExcavareMain()
	program.excavare.show()
	gtk.main()
	
####
####
#CODE THATS PROBABLY USEFUL SOMEDAY
####
#	def on_quit_menu_item_activate(self,quit_menu_item):
#		textbuff=self.textview.get_buffer()
#		if textbuff.get_modified():
#			self.dialog.show()
#		else:
#			gtk.main_quit()
#		#textbuff.set_modified(False)	
		#the above resets the text buffer, I don't want
		#to lose the code but neither of these situations
		#should reset the buffer, which should ONLY be 
		#reset after a save
#	def on_quit_Ok_clicked(self,quit_Ok):
#		gtk.main_quit()
#	def on_quit_Cancel_clicked(self,quit_Cancel):
#		self.dialog.hide()

#		textbuff=self.textview.get_buffer()
#		if textbuff.get_modified():
#			print 'Something changed'
#		else:
#			print 'No change'
#		textbuff.set_modified(False)
